//
//  ZZView.m
//  BezierDemo
//
//  Created by yuanjiang on 2017/12/19.
//  Copyright © 2017年 yuanjiang. All rights reserved.
//

#import "ZZView.h"

#define margin 30
#define KWidth self.bounds.size.width
#define KHeight self.bounds.size.height

@implementation ZZView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (void)creatZhuZhuangTuWithXarr:(NSArray *)x_arr
                            Yarr:(NSArray *)y_arr
{
    float max, min;
    if (_MAX_Value) {
        max = _MAX_Value;
    }else{
        max = 101;
    }
    if (_MIN_Value) {
        min = _MIN_Value;
    }else{
        min = -1;
    }
    //建立坐标轴
    [self creatZuoBiaoZhouWithArr:x_arr];
    //画柱状图
    [self creatZhuZhuangTuWitharray:y_arr min:min max:max];
}

- (void)creatZhuZhuangTuWitharray:(NSArray *)arr min:(CGFloat)min max:(CGFloat)max
{
    for (int i = 0; i < arr.count; i++) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(2* margin + 1.5*margin*i, KHeight - margin - 3*[arr[i] floatValue], 0.8*margin, 3*[arr[i] floatValue])];
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.path = path.CGPath;
        UILabel *textLab = [[UILabel alloc]initWithFrame:CGRectMake(2*margin+1.5*margin*i, KHeight-margin-3*[arr[i] floatValue]-20, 0.8*margin, 20)];
        textLab.text = arr[i];
        [self addSubview:textLab];
        
        if ([arr[i] floatValue]<= min) {
            layer.fillColor = [UIColor redColor].CGColor;
        }else if ([arr[i] floatValue]>=max){
            layer.fillColor = [UIColor greenColor].CGColor;
        }else{
            layer.fillColor = [UIColor blueColor].CGColor;
        }
        //        layer.fillColor = zzRandomColor.CGColor;
        layer.strokeColor = [UIColor clearColor].CGColor;
        [self.layer addSublayer:layer];
    }
}

/**
 创建坐标系

 @param x_arr nil
 */
- (void) creatZuoBiaoZhouWithArr:(NSArray *)x_arr
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPath];
    //原点
    CGPoint rPoint =  CGPointMake(margin, KHeight - margin);
    //y轴
    [path moveToPoint:rPoint];
    [path addLineToPoint:CGPointMake(margin, margin)];
    //y轴的箭头
    [path moveToPoint:CGPointMake(margin, margin)];
    [path addLineToPoint:CGPointMake(margin - 5, margin + 5)];
    [path moveToPoint:CGPointMake(margin, margin)];
    [path addLineToPoint:CGPointMake(margin + 5, margin + 5)];
    // x 轴
    [path moveToPoint:rPoint];
    [path addLineToPoint:CGPointMake(KWidth - margin , KWidth - margin)];
    //x轴的箭头
    [path addLineToPoint:CGPointMake(KWidth - margin - 5, KWidth - margin + 5)];
    [path moveToPoint:CGPointMake(KWidth - margin, KWidth - margin)];
    [path addLineToPoint:CGPointMake(KWidth - margin - 5, KWidth - margin - 5)];
    //x轴上的标度
    for (int i = 0; i< x_arr.count; i++) {
        [path moveToPoint:CGPointMake(2 * margin + 1.5 * margin * i, KHeight - margin)];
        [path addLineToPoint:CGPointMake(2 * margin + 1.5 * margin * i, KHeight - margin - 3)];
    }
    //y轴上的标度
    for (int i =0; i < 10; i++) {
        [path moveToPoint:CGPointMake(margin, KHeight - 2*margin - margin* i)];
        [path addLineToPoint:CGPointMake(margin + 3, KHeight - 2*margin - margin* i)];
    }
    
    [path moveToPoint:rPoint];
    layer.path = path.CGPath;
    layer.lineWidth = 2.0;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    [path stroke];
    [self.layer addSublayer:layer];
    
    //y轴加标注
    for (int i = 0; i< 11; i++) {
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(margin  -25, KHeight - margin - margin*i - 10, 25, 20)];
        lab.text = [NSString stringWithFormat:@"%d",10 *i];
        lab.textColor =[UIColor blackColor];
        lab.font = [UIFont systemFontOfSize:12];
        lab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:lab];
    }
    //x轴标注
    for (int i = 0; i< x_arr.count; i++) {
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(2*margin + 1.5*margin*i, KHeight - margin, margin ,20)];
        lab.text = x_arr[i];
        lab.textColor =[UIColor blackColor];
        lab.font = [UIFont systemFontOfSize:12];
        lab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:lab];
    }
    
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    
    
}


@end
